package com.ycfmedical.bed.base;

import com.bilibili.magicasakura.widgets.TintImageView;
import com.bilibili.magicasakura.widgets.TintTextView;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.provider.Card;
import com.ycfmedical.bed.provider.CardViewProvider;
import com.ycfmedical.bed.provider.Category;
import com.ycfmedical.bed.provider.CategoryViewProvider;
import com.ycfmedical.bed.provider.Contributor;
import com.ycfmedical.bed.provider.ContributorViewProvider;
import com.ycfmedical.bed.provider.License;
import com.ycfmedical.bed.provider.LicenseViewProvider;
import com.ycfmedical.bed.provider.Line;
import com.ycfmedical.bed.provider.LineViewProvider;

import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * @author Egoist
 */
public abstract class BaseDetailActivity<P extends IPresent> extends XActivity implements View.OnClickListener
{
    
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    
    @BindView(R.id.list)
    RecyclerView recyclerView;

    @BindView(R.id.ic_icon)
    TintImageView icIcon;
    
    @BindView(R.id.tv_title)
    TintTextView tvTitle;
    
    @BindView(R.id.tv_author)
    TintTextView tvAuthor;
    
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    protected Items items;

    protected MultiTypeAdapter adapter;

    protected abstract void onCreateHeader(ImageView icon, TextView title, TextView author);
    
    protected abstract void onItemsCreated(@NonNull Items items);
    
    @Nullable
    protected CharSequence onCreateTitle()
    {
        return null;
    }
    
    protected void onActionClick(View action)
    {
    }
    
    @Override
    public void initData(Bundle savedInstanceState)
    {
        onCreateHeader(icIcon, tvTitle, tvAuthor);
        initTitle();
        initToobar();
        onSetupRecyclerView(recyclerView);
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_healthdetail;
    }

    
    protected void initTitle()
    {
        
        final CharSequence title = onCreateTitle();
        if (title != null)
        {
            collapsingToolbar.setTitle(title);
        }
    }
    
    protected void initToobar()
    {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    
    private void onSetupRecyclerView(RecyclerView recyclerView)
    {
        adapter = new MultiTypeAdapter();
        adapter.register(Category.class, new CategoryViewProvider());
        adapter.register(Card.class, new CardViewProvider(this));
        adapter.register(Line.class, new LineViewProvider());
        adapter.register(Contributor.class, new ContributorViewProvider());
        adapter.register(License.class, new LicenseViewProvider());
        items = new Items();
        onItemsCreated(items);
        adapter.setItems(items);
        recyclerView.setAdapter(adapter);
    }
    
    /**
     * Set the header view background to a given resource and replace the default value ?attr/colorPrimary. The resource
     * should refer to a Drawable object or 0 to remove the background.
     *
     * @param resId The identifier of the resource.
     */
    public void setHeaderBackgroundResource(@DrawableRes int resId)
    {
        if (collapsingToolbar == null)
        {
            collapsingToolbar = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
        }
        collapsingToolbar.setContentScrimResource(resId);
        collapsingToolbar.setBackgroundResource(resId);
    }
    
    public void setHeaderContentColor(@ColorInt int color)
    {
        collapsingToolbar.setCollapsedTitleTextColor(color);
        tvTitle.setTextColor(color);
        tvAuthor.setTextColor(color);
    }
    
    /**
     * Set the icon to use for the toolbar's navigation button.
     *
     * @param resId Resource ID of a drawable to set
     */
    public void setNavigationIcon(@DrawableRes int resId)
    {
        toolbar.setNavigationIcon(resId);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem)
    {
        if (menuItem.getItemId() == android.R.id.home)
        {
            onBackPressed();
        }
        return super.onOptionsItemSelected(menuItem);
    }
    
    @Override
    public void setTitle(CharSequence title)
    {
        collapsingToolbar.setTitle(title);
    }
    
    @Override
    public void onClick(View v)
    {
        int id = v.getId();
        if (id == R.id.action)
        {
            onActionClick(v);
        }
    }

}
